﻿using Xamarin.Forms.Platform.iOS;
using UIKit;
using Xamarin.Forms;
using Moven.iOS.Effects;
using System;

[assembly: ResolutionGroupName("Moven")]
[assembly: ExportEffect(typeof(LightFontLabelEffect), "LightFontLabelEffect")]

namespace Moven.iOS.Effects
{
	public class LightFontLabelEffect: PlatformEffect
	{
		protected override void OnAttached()
		{
			var label = (UILabel)Control;
			var fontName = label.Font.Name;
			if (fontName.EndsWith("-Light", StringComparison.InvariantCulture) &&
				fontName.EndsWith("-UltraLight", StringComparison.InvariantCulture) &&
				fontName.EndsWith("-Thin", StringComparison.InvariantCulture) &&
				fontName.EndsWith("-Regular", StringComparison.InvariantCulture) &&
				fontName.EndsWith("-Medium", StringComparison.InvariantCulture) &&
				fontName.EndsWith("-Semibold", StringComparison.InvariantCulture) &&
			    fontName.EndsWith("-Bold", StringComparison.InvariantCulture))
			{
				var index = fontName.LastIndexOf("-", StringComparison.InvariantCulture);
				fontName = fontName.Substring(0, index);
			}
			    
			var font = UIFont.FromName(label.Font.Name + "-Light", label.Font.PointSize);
			if (font != null)
			{
				label.Font = font;
			}
		}

		protected override void OnDetached()
		{
		}
	}
}
